package com.genius.adminmanager.user.service.impl;

import java.sql.SQLException;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.adminmanager.user.service.UserServiceAdapter;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;
import com.genius.workflow.util.GeniusJsonUtil;

@Service("userTableSelectService")
public class UserTableSelectServiceImpl extends UserServiceAdapter{

	@Override
	public JSONObject getTableSelect(JSONObject json, JSONObject response) {
		// TODO Auto-generated method stub
		int limit = json.getIntValue(PageResult.limit);
        int page = json.getIntValue(PageResult.page);
        SqlLang sql = null;
        String headField = json.getString(PageResult.headField);
        String selectSB = "SELECT userId,username,a.name,password,command,sex,phone,mail,createTime,a.status,b.name groupname FROM t_user a left join t_group b on a.groupId=b.groupId where a.userId!=?";
        
        JSONObject searchJson = new JSONObject();
    	searchJson.put(UserJson.userId, "000");
    	String parameterArr [] = DBTools2.parsePermeterBySql(selectSB,searchJson);        
        //进行排序
        if(json.containsKey(PageResult.asc)){
            String ascField = json.getString(PageResult.ascField);
            String asc = json.getString(PageResult.asc);
           
            sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc);
            
        }else{
        	 String asc = "desc";
             String ascField = UserJson.createTime;
             sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc);
        }
        try {
            PageResult result = DBUtils.selectByCount(sql,parameterArr);
            JSONArray data = (JSONArray)result.getData();
            for(int i =0;i<data.size();i++){
                GeniusJsonUtil.convertResource(data.getJSONObject(i),UserJson.resource);
            }
            response.put(ResponseJson.count, result.getCount());
            response.put(ResponseJson.data, data);
            response.put(ResponseJson.status, ResponseJson.status_Failed);
        } catch (ConnectionException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
        } catch (DBClosedException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
        } catch (SQLException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
        }
		return response;
	}
	
}
